package dataStructure.Queue;

import java.util.Scanner;

/**
 * @author lhn
 * @date 2024-04-12 10:32
 */

public class CircleArrayQueue {
    private int maxSize; // 数组最大容量
    private int front = 0;  //  队列头
    private int rear = 0;   //  指向队列尾部，指向队列尾的后一个位置
    private int[] arr;  //  该数据的存入位置

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
    }
    public boolean isFull() {
        return (this.rear + 1) % this.maxSize == this.front;
    }
    public boolean isEmpty() {
        return this.front == this.rear;
    }
    public void addQueue(int n) {
        if (isFull()){
            System.out.println("队列已满不能加入数据");
            return;
        }
        this.arr[rear] = n;
        rear = (rear + 1) % maxSize;    //  循环队列
    }
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能取数据");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }
    public int size() {
//        System.out.println("size() is used size is " + (rear + maxSize - front) % maxSize);
        return (rear + maxSize - front) % maxSize;
    }
    public void showQueue() {
        if (isEmpty()){
            System.out.println("队列为空");
            return;
        }
        for (int i = this.front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n",i % maxSize, arr[i % maxSize]);
        }

    }
    public int headQueue() {
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        } else {
            return this.arr[front];
        }
    }

    public static void main(String[] args) {
        //创建一个队列
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
        char Key = ' ';//接口用户输入
        Scanner s= new Scanner(System.in);
        boolean loop = true;
        //输出菜单
        while(loop){
            System.out.println("s:显示队列");
            System.out.println("e:退出程序");
            System.out.println("a:添加数据到队列");
            System.out.println("g:从队列取出数据");
            System.out.println("h:查看队列头部数据");
            Key = s.next().charAt(0);//接收一个字符
            switch (Key){
                case  's':
                    circleArrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value= s.nextInt();
//                    System.out.println("value is " + value);
                    circleArrayQueue.addQueue(value);
                    break;
                case 'g':
                    try{
                        int res = circleArrayQueue.getQueue();
                        System.out.println("去除的数据为："+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());//捕捉异常信息
                    }
                    break;
                case 'h':
                    try{
                        int res = circleArrayQueue.headQueue();
                        System.out.println("头部队列的数据为："+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());//捕捉异常信息
                    }
                    break;
                case 'e':
                    s.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }
        System.out.println("程序退出");
    }
}
